from PySide6.QtCore import QThread,Signal
from PySide6.QtWidgets import QMessageBox,QMainWindow,QApplication

class caseTranform:
    def __init__(self,num):
        self.num = num
        self.integer_part = 0
        self.decimal_part = 0
        # 定义数字和中文大写数字的对应关系
        self.num_dict = {
            0: '零', 1: '壹', 2: '贰', 3: '叁', 4: '肆', 5: '伍', 6: '陆',
            7: '柒', 8: '捌', 9: '玖'
        }
        # 定义单位对应关系
        self.unit_dict = ['', '拾', '佰', '仟']
        # 定义万级单位对应关系
        self.wan_unit_dict = ['', '万', '亿', '兆']
        self.integerAnddecimal()


    def integerAnddecimal(self):
        # 分离整数与两位小数
        try:
            float(self.num)
        except:
            raise ValueError("你输入的不是数字！")

        try:
            num = str(self.num).split('.')
            self.integer_part = int(num[0])
            self.decimal_part = num[1]
        except:
            self.integer_part = str(self.num)

        if len(str(self.integer_part)) > 16:
            raise ValueError("输入的数字过大！")

    def convert_number(self):
        # 整数部分
        parts = []
        num = str(self.integer_part)
        for i in range(0,len(num),4):
            part = num[::-1][i:i + 4]  # 取每四位一组并反转回来
            parts.append(part)  # 不足四位用0填充
        result = []
        wan_unit_index = 0
        for part in parts:
            converted_part = self.convert_part(part[::-1])
            if converted_part != self.num_dict[0]:
                result.append(converted_part + self.wan_unit_dict[wan_unit_index])
            else:
                result.append(converted_part)
            wan_unit_index += 1
        result = result[::-1]
        # result = ''.join(result)
        try:
            i = result.index('零')
            try:
                if result[i+1]=='零':
                    result.pop(i+1)
                if result[i+1]=='零':
                    result.pop(i+1)
            except:
                pass
        except:
            pass
        if result[-1] == '零':
            result.pop(-1)
        result += '元'
        result = ''.join(result)
        # 小数部分
        num_str = str(self.decimal_part)
        if int(self.decimal_part) == 0:
            if int(self.integer_part)!=0:
                result += '整'
        else:
            decimal_part = ''
            if len(num_str) ==1:
                if num_str != '0':
                    decimal_part = self.num_dict[int(num_str)] + '角'
            else:
                if num_str[0] != '0':
                    decimal_part = self.num_dict[int(num_str[0])] + '角'
                if num_str[1] != '0':
                    decimal_part += self.num_dict[int(num_str[1])] + '分'
            result += decimal_part
        return result

    def convert_part(self,part):
        """将数字的某一部分转换为中文大写"""
        if int(part) == 0:
            return self.num_dict[0]
        result = []
        zero_flag = False
        part_len = len(part)
        for i in range(part_len - 1, -1, -1):
            digit = part[i]
            unit_dict = self.unit_dict[:part_len]
            if digit != '0':
                if zero_flag:
                    result.insert(0, self.num_dict[0])
                    zero_flag = False
                result.insert(0, self.num_dict[int(digit)] + unit_dict[-(i+1)])

            else:
                zero_flag = True
        if result[-1] == self.num_dict[0]:
            result.pop(-1)
        return ''.join(result) or self.num_dict[0]

class caseChangeThread(QThread):
    trigger = Signal(object)
    def __init__(self,num):
        super().__init__()
        self.num = num

    def run(self):
        try:
            result = caseTranform(self.num).convert_number()
            print(result)
            # self.trigger.emit(result)
        except ValueError  as e:
            result = {'err':e}
            # self.trigger.emit(result)


worker = caseTranform('8000000010000000')
a = worker.convert_number()
print(a,'a')
# class MainWindow(QMainWindow):
#     def __init__(self):
#         super().__init__()
#         self.worker = caseChangeThread('988654775')
#         self.w = self.worker.trigger.connect(self.handle_finished)
#         self.worker.start()
#
#     def handle_finished(self, result):
#         print(f"计算结果: {result}")
#         return result
#
#
#
# # 应用程序入口
# app = QApplication([])
# window = MainWindow()
# window.show()
# app.exec()
